export default {
  props: {

  },
  data () {
    return {
      selectSpec: {}, // 选择数据的对象 将已选的数据放在这个对象里面记录下来  用对象的好处在下面深拷贝处就能体验到了
      specList: [],
      matchingStr: '',
      matching: {}, // 匹配 SKU
      currentRow: {},
      orderDetailList: [] // 已选择商品
    }
  },
  computed: {

  },
  created () {
    // 获取字典值
    // for (const type in this.dictionaries) { this.getDictionaries(type) }

  },
  methods: {
    // 点击多规格组合弹框内容
    moreCombination (row) {
      console.log(row, '点击多规格组合弹框内容')
      this.currentRow = row
      this.matchingStr = ''
      this.matching = {}
      this.moreSpecification = row// 吧row保存下来 用于给弹框点击确定的时候执行添加方法
      this.selectSpec = {}

      row.skuList.map((it) => {
        console.log(it, 'it')
        it.skuArr = []
        it.skuArr.push(it.firstGoodsSpecItemContent)
        it.skuArr.push(it.secondGoodsSpecItemContent)
        it.skuArr.push(it.thirdGoodsSpecItemContent)
        it.skuArr = it.skuArr.filter(Boolean)
      })
      row.goodsSpecList.forEach(item => {
        this.$set(this.selectSpec, item.name, '')
      })
      this.skuList = row.skuList
      row.goodsSpecList.map(item => {
        item.specItemList.map(its => {
          its.able = this.isAble(item.name, its.content) // 注释的调试看逻辑代码 false
          //  判断是否可以选择
          //  这里相当于init 初始化数据  this.isAble() 核心判断逻辑
        })
      })
      this.specList = row.goodsSpecList
      if (row.skuList.length === 1) {
        this.addSelectList('only')
        return console.log('直接添加')
      }
      this.dialog.visible = true
      this.dialog.title = '选择规格'
      this.dialog.type = 'moreSpecification'
      this.dialog.width = '650px'
    },
    // 点击事件
    changeSpec (key, value) {
      if (this.selectSpec[key] === value) {
        this.selectSpec[key] = ''
      } else {
        this.selectSpec[key] = value
      }
      this.specList.forEach(item => {
        item.specItemList.forEach(its => {
          its.able = this.isAble(item.name, its.content)
        })
      })
      // str.split('')
      // this.matchingStr.push(`${key}/${value}`)
      console.log(`${key}/${value}`)
      console.log(this.selectSpec)
      console.log(this.specList)
      // console.log(this.skuList)
      const selectSpec = this.selectSpec
      const selectSpecValue = []
      for (const key in this.selectSpec) {
        if (selectSpec[key]) { selectSpecValue.push(selectSpec[key]) }
      }
      // selectSpecValue = selectSpecValue.reverse()
      const tempSkuList = this.skuList
      let tempStr = ''
      for (let i = 0; i < tempSkuList.length; i++) {
        const isArrmatching = this.isArrEqual(tempSkuList[i].skuArr, selectSpecValue)
        if (isArrmatching) {
          this.matching = tempSkuList[i]
          this.matching.goodsSkuId = tempSkuList[i].id
          this.matching.discountAmount = '0.00'
          for (const key in this.selectSpec) {
            tempStr += key + '/' + this.selectSpec[key] + '&ensp;'
          }
          this.matchingStr = tempStr
          this.matching.specTitle = tempStr
          // this.matching.quantity = 1
          this.matching.mainImageArr = []
          this.matching.mainImageArr.push(this.currentRow.mainImage)
          this.matching.mainImage = this.currentRow.mainImage
          break
          // throw new Error('LoopInterrupt')
        } else {
          this.matching = {}
          this.matchingStr = ''
        }
      }
    },
    isArrEqual (arr1, arr2) {
      // 判断一个数组是否包含一个指定的值
      return arr1.length === arr2.length && arr1.every((ele) => arr2.includes(ele))
    },
    isAble (key, value) {
      // 深拷贝 避免被影响
      var copySelectSpec = JSON.parse(JSON.stringify(this.selectSpec))
      // 用对象的好处就在这了 直接赋值当前验证项
      copySelectSpec[key] = value
      // 用数组的 some 方法 效率高 符合条件直接退出循环
      const flag = this.skuList.some(item => {
        // 条件判断 核心逻辑判断
        // console.log(item)
        var i = 0
        // 这个for in 循环的逻辑就对底子不深的人来说就看不懂了 原理就是循环已经选中的 和 正在当前对比的数据 和 所有的sku对比 只有当前验证的所有项满足sku中的规格或者其他规格为空时 即满足条件 稍微有点复杂 把注释的调试代码打开就调试下就可以看懂了
        for (const k in copySelectSpec) {
          // 注释的调试看逻辑代码
          if (copySelectSpec[k] != '' && item.skuArr.includes(copySelectSpec[k])) {
            i++
          } else if (copySelectSpec[k] == '') {
            i++
          }
        }
        // 符合下面条件就退出了 不符合会一直循环知道循环结束没有符合的条件就 return false 了
        return i == this.specList.length
      })
      return flag
    },
    addSelectList (sku) {
      console.log(this.$route.query.type, 'this.$route.query.type')
      // debugger
      // const tempObj = Object.assign(this.currentRow, this.matching)
      let isOnly = false
      if (sku === 'only') {
        isOnly = true
      } else {
        isOnly = false
      }
      const currentRow = this.currentRow
      const matching = isOnly ? currentRow : this.matching
      console.log(this.matching, 'this.matching')
      console.log(this.currentRow, 'this.currentRow')
      if (JSON.stringify(matching) === '{}') { return this.$message.warning(`请选择规格！`) }
      // debugger
      const tempObj = {
        goodsId: currentRow.id,
        name: currentRow.name,
        goodsType: currentRow.goodsType,
        goodsNo: currentRow.goodsNo,
        goodsSkuId: isOnly ? currentRow.skuList[0].id : matching.goodsSkuId,
        specTitle: isOnly ? `${currentRow.goodsSpecList[0].name} ${currentRow.goodsSpecList[0].specItemList[0].content}` : matching.specTitle,
        marketPrice: isOnly ? currentRow.skuList[0].marketPrice : matching.marketPrice,
        totalMarketPrice: isOnly ? currentRow.skuList[0].marketPrice : matching.marketPrice,
        retailPrice: isOnly ? currentRow.skuList[0].retailPrice : matching.retailPrice,
        totalRetailPrice: isOnly ? currentRow.skuList[0].retailPrice : matching.retailPrice,
        mainImageArr: [],
        quantity: 1,
        discountAmount: 0.00

      }
      if (this.$route.query.type === 1) {
        tempObj.barnd = currentRow.brandName
        tempObj.type = currentRow.categoryName
      }
      tempObj.mainImageArr.push(currentRow.mainImage)
      tempObj.mainImage = currentRow.mainImage
      console.log(tempObj)
      const orderDetailList = this.orderDetailList
      const indexLength = orderDetailList.length
      if (indexLength > 0) {
        let isAdd = false
        for (let i = 0; i < indexLength; i++) {
          if (orderDetailList[i].goodsSkuId === tempObj.goodsSkuId) {
            this.orderDetailList[i].quantity = orderDetailList[i].quantity + 1
            isAdd = true
            break
          }
        }
        if (!isAdd) {
          this.$set(this.orderDetailList, indexLength, tempObj)
        }
      } else {
        this.$set(this.orderDetailList, indexLength, tempObj)
        // this.orderDetailList.unshift(tempObj)
      }
      // this.orderDetailList.push(tempObj)
      this.dialog.visible = false
      this.orderDetailList = this.orderDetailList.sort((star, next) => {
        const order = ['普通商品', '赠品']
        return order.indexOf(star.goodsType) - order.indexOf(next.goodsType)
      }
      )
      console.log(this.orderDetailList, 'this.orderDetailList')
      this.$message.success('添加商品成功！')
    },
    // 添加商品、赠品
    addOrder (row) {
      const rows = {
        // 初始化默认状态
        goodsType: row.goodsType,
        name: row.name,
        specification: row.specification,
        goodsSpecList: row.goodsSpecList,
        marketPrice: row.marketPrice,
        id: row.id,
        skuList: row.skuList,
        number: 1,
        mainImage: row.mainImage,
        specificationName: row.specificationName,
        title: row.title,
        total: 1 * parseInt(row.marketPrice),
        brandId: row.brandId,
        typeId: row.typeId
      }
      let numb = 0
      let price = 0

      // 判断商品是否重复
      if (this.tableData.length !== 0) {
        // 有第一个参数但是arry没有
        if (this.arry.indexOf(row.id) >= 0) {
          let setNum = 0
          let indexTab = 0
          // 获取需要改变数量得值
          for (let i = 0; i < this.tableData.length; i++) {
            if (this.tableData[i].id == row.id) {
              setNum = this.tableData[i].number + 1
              indexTab = i
            }
          }
          // this.total = setNum * parseInt(row.marketPrice)
          // 获取需要改变数量得值
          this.$set(this.tableData, indexTab, {
            goodsType: row.goodsType,
            specification: row.specification,
            name: row.name,
            goodsSpecList: row.goodsSpecList,
            marketPrice: row.marketPrice,
            mainImage: row.mainImage,
            specificationName: row.specificationName,
            createdTime: row.createdTime,
            id: row.id,
            skuList: row.skuList,
            number: setNum,
            sellPrice: row.sellPrice,
            title: row.title,
            total: setNum * parseInt(row.marketPrice),
            brandId: row.brandId,
            typeId: row.typeId
          })
        } else {
          this.arry = []
          this.tableData.push(rows)
          // 不相同则需要插入id
          for (let i = 0; i < this.tableData.length; i++) {
            this.arry.push(this.tableData[i].id)
          }
          // 不相同则需要插入id
        }
      } else {
        // tableData一开始为空的 添加了rows后有值
        this.arry = [] // 初始化一次再push
        this.tableData.push(rows)
        for (let i = 0; i < this.tableData.length; i++) {
          this.tableData[i].total =
            Number(this.tableData[i].number) * Number(this.tableData[i].marketPrice)
          this.arry.push(this.tableData[i].id) // 需要集合商品id
        }
      }
      // 判断商品是否重复
      // 赠品数量
      const zpNum = 0
      const zpPrice = 0
      for (let i = 0; i < this.tableData.length; i++) {
        numb += Number(this.tableData[i].number)
        price += Number(this.tableData[i].total)
      }
      this.orderNumber = Number(numb)
      this.origTotal = price + zpPrice
      this.total = this.origTotal
      this.giftPrice = zpPrice.toFixed(2)
    },
    // 删除
    delectSelect (index) {
      const goodsName = this.orderDetailList[index].name
      this.orderDetailList.splice(index, 1)
      this.$message.warning(`商品${goodsName}被移除！`)
      // this.arry.splice(index, 1)
      // this.calculation()
    }
  }
}
